import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../core/providers/global_state_coordinator.dart';
import '../core/providers/hot_item_provider.dart';
import '../core/routes/app_router.dart';
import '../core/routes/navigation_helper.dart';
import '../core/services/reminder_service.dart';

/// 应用主体
class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      ReminderService().setContext(context);
      // 初始化HotItemProvider
      context.read<HotItemProvider>().initialize();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<GlobalStateCoordinator>(
      builder: (context, appProvider, child) {
        return ScreenUtilInit(
          designSize: const Size(375, 812),
          minTextAdapt: true,
          splitScreenMode: true,
          builder: (context, child) {
            return MaterialApp(
              title: 'Qilo',
              debugShowCheckedModeBanner: false,
              theme: _buildTheme('light'),
              navigatorKey: NavigationHelper.navigatorKey,
              initialRoute: AppRouter.splash,
              onGenerateRoute: AppRouter.generateRoute,
              builder: (context, child) {
                ReminderService().setContext(context);
                return child!;
              },
            );
          },
        );
      },
    );
  }

  ThemeData _buildTheme(String themeName) {
    switch (themeName) {
      case 'dark':
        return ThemeData(
          brightness: Brightness.dark,
          primarySwatch: Colors.blue,
          useMaterial3: true,
          appBarTheme: const AppBarTheme(
            backgroundColor: Colors.black87,
            foregroundColor: Colors.white,
          ),
        );
      case 'light':
      default:
        return ThemeData(
          brightness: Brightness.light,
          primarySwatch: Colors.blue,
          useMaterial3: true,
          appBarTheme: const AppBarTheme(
            backgroundColor: Colors.blue,
            foregroundColor: Colors.white,
          ),
        );
    }
  }
}
